<template>
  <el-card class="SystemParameterConfiguration">
    <el-button
      type="primary"
      icon="el-icon-circle-plus-outline"
      @click="dialogVisible = true"
      >新增</el-button
    >
    <div class="divOne">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="name" label="名称" width="600">
        </el-table-column>
        <el-table-column prop="key_name" label="Key" width="600">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="configQueryOneOpEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" @click="delbtn(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 新增 -->
    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="40%"
      style="margin-top: -8vh"
    >
      <el-input v-model="name" placeholder="请输入名称"></el-input>
      <el-input v-model="key_name" placeholder="请输入Key"></el-input>
      <!-- <el-input
        type="textarea"
        :rows="6"
        v-model="value"
        placeholder="请输入值"
      ></el-input> -->
      <vue-json-editor
        v-model="value"
        :showBtns="false"
        :mode="'code'"
        lang="zh"
        @has-error="onError"
      />

      <el-input
        type="textarea"
        :rows="10"
        v-model="remarks"
        placeholder="请输入备注"
      ></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="configSetConfigBtn">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisibleB"
      width="40%"
      style="margin-top: -8vh"
    >
      <el-input v-model="nameB" placeholder="请输入名称"></el-input>
      <el-input v-model="key_nameB" placeholder="请输入Key" disabled></el-input>
      <!-- <el-input
        type="textarea"
        :rows="5"
        v-model="valueB"
        placeholder="请输入值"
      ></el-input> -->

      <vue-json-editor
        v-model="valueB"
        :showBtns="false"
        :mode="'code'"
        lang="zh"
        @has-error="onError"
      />
      <!-- 
      <json-viewer
        :value="valueC"
        :expand-depth="5"
        copyable
        boxed
      ></json-viewer> -->

      <el-input
        type="textarea"
        :rows="10"
        v-model="remarksB"
        placeholder="请输入备注"
      ></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleB = false">取 消</el-button>
        <el-button type="primary" @click="configQueryOneOpBtn">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  configQueryList,
  configSetConfig,
  configQueryOne,
  configDelete,
} from "@/api/SystemManagement";
import qs from "qs";
import vueJsonEditor from "vue-json-editor";

export default {
  inject: ["reload"],
  data() {
    return {
      //新增
      dialogVisible: false,
      name: "",
      key_name: "",
      value: "",
      remarks: "",
      //编辑
      dialogVisibleB: false,
      nameB: "",
      key_nameB: "",
      valueB: "",
      valueC: "",
      remarksB: "",

      tableData: [
        {
          id: "",
          key_name: "",
          name: "",
          value: "",
          remarks: "",
        },
      ],
      // 当前页码
      currentPage: 1,
      // 总数据条数
      total: 0,
      // 每页数量
      pageSize: 10,
    };
  },
  components: {
    vueJsonEditor,
  },
  methods: {
    //列表
    configQueryList() {
      configQueryList({
        name: null,
        key: null,
        page: this.currentPage,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.code == 0) {
          this.total = res.data.count;
          this.tableData = res.data.list;
        }
      });
    },
    //新增
    configSetConfigBtn() {
      var data = { data: {} };
      data.data = this.value;
      configSetConfig({
        key: this.key_name,
        name: this.name,
        value: data,
        remarks: this.remarks,
      }).then((res) => {
        if (res.code == 0) {
          this.dialogVisible = false;
          this.reload();
        }
      });
    },
    //编辑
    configQueryOneOpEdit(index, row) {
      configQueryOne({
        key: row.key_name,
      }).then((res) => {
        if (res.code == 0) {
          this.dialogVisibleB = true;
          var valueData = JSON.parse(res.data.value);
          this.nameB = res.data.name;
          this.key_nameB = res.data.key_name;

          // this.valueB = JSON.stringify(valueData.data);
          this.valueB = valueData;
          // this.valueB = JSON.stringify(innerData).replace("\"","").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"").replace('\\',"");
          // this.valueC = valueData; //格式化
          this.remarksB = res.data.remarks;
          // var reg = new RegExp(/\"/g, "");
          // this.valueB = this.valueB.replace(reg, "");
        }
      });
    },
    configQueryOneOpBtn() {
      // var data = { data: {} };
      // data.data = this.valueB;
      // console.log(JSON.stringify(data));
      // return
      configSetConfig(
        qs.stringify({
          key: this.key_nameB,
          name: this.nameB,
          value: this.valueB,
          remarks: this.remarksB,
        })
      ).then((res) => {
        if (res.code == 0) {
          this.dialogVisibleB = false;
          this.reload();
        }
      });
    },
    // 每页条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.configQueryList();
    },
    // 当前页码
    handleCurrentChange(val) {
      this.currentPage = val;
      this.configQueryList();
    },
    //JSON格式编辑错误提示
    onError(value) {
      if (value !== "") {
        this.$message({
          type: "info",
          message: "格式有误,请正确编辑数据",
        });
      }
    },
    delbtn(index, obj) {
      this.$confirm("此操作将永久删除该配置, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          configDelete({
            key: obj.key_name,
          }).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功！",
              });
              this.configQueryList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.configQueryList();
  },
};
</script>

<style lang="less" scoped>
.SystemParameterConfiguration {
  height: 100%;
  margin: 30px;
  .divOne {
    margin-top: 20px;
    .el-pagination {
      text-align: center;
      margin-top: 20px;
      .el-icon-arrow-right:before,
      .el-icon-arrow-left:before {
        display: none;
      }
      .btn-prev:before {
        content: "上一页";
      }
      .btn-next:before {
        content: "下一页";
      }
    }
  }
  /deep/.jsoneditor-vue {
    height: 300px;
  }
  /deep/.jsoneditor-modes {
    display: none;
  }
}
</style>